package cn.fl.inscare.controller;

import cmm.mid.core.framework.page.ListPageVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.utils.*;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.inscare.service.IInsPayInfoService;
import cn.fl.inscare.vo.InsUserPayDetailVO;
import ins.car.facade.car.car.condition.InsPayInfoQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 保险支付信息表 控制层
 */
@Controller
@RequestMapping("/ins/pay/")
public class InsPayInfoController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(InsPayInfoController.class);

    /**
     * InsPayInfo 服务层BEAN
     */
    @Autowired
    private IInsPayInfoService insPayInfoService;


    /**
     * 获取支付详情
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "getPayDetail.do", method = RequestMethod.GET)
    @ResponseBody
    public Object getPayDetail(HttpServletRequest request) {
        Long startTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getPayDetail", "根据保险申请表id获取保险支付详情无数据：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());

        String applyId = request.getParameter("id");
        //结果list
        List<InsUserPayDetailVO> resultList = new ArrayList<>();
        try {
            if (StringUtils.isNotEmpty(applyId)) {
                InsPayInfoQC qc = new InsPayInfoQC();
                qc.setApplicationId(Long.valueOf(applyId));
                //根据保险申请表id获取保险支付详情数据
                resultList = insPayInfoService.searchPayInfoByQc(qc);
		/*List<InsPayInfoVO> payInfoList =
                	searchPayInfoByQc;*/
                if (CollectionUtils.isEmpty(resultList)) {
                    BaseLogUtils.info(logger, "getPayDetail", "根据保险申请表id获取保险支付详情无数据" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
                    return null;
                }
                for (InsUserPayDetailVO vo : resultList) {
                    vo.setInterestAmtNm(DecimalUtils.convertFenToYuan(vo.getInterestAmt()));
                    //本金转换
                    vo.setPrincipalAmtNm(DecimalUtils.convertFenToYuan(vo.getPrincipalAmt()));
                    //剩余应还合计金额转换
                    vo.setRemainRepayTotalAmtNm(DecimalUtils.convertFenToYuan(vo.getRemainRepayTotalAmt()));
                    //租金转换
                    vo.setRentAmtNm(DecimalUtils.convertFenToYuan(vo.getRentAmt()));
                    //还款日转换
                    vo.setRepaymentDtNm(DateUtils.formatDateToString(vo.getRepaymentDt(), "yyyy-MM-dd"));
                }
            }
        } catch (Exception ex) {
            BaseLogUtils.info(logger, "getPayDetail", "根据保险申请表id获取保险支付详情无数据", ex);
        }
        Long endTm = System.currentTimeMillis();
        BaseLogUtils.info(logger, "getPayDetail", "根据保险申请表id获取保险支付详情无数据.耗时：" + (endTm - startTm), CurrentThreadContext.getCurrentUserName());
        ListPageVO<InsUserPayDetailVO> listPage = new ListPageVO<InsUserPayDetailVO>(resultList, resultList.size());
        return listPage;
    }

}

